<?php
require_once(dirname(__FILE__).'/simpletest/autorun.php');

if (!defined('ROOT_PATH')) {
   define('ROOT_PATH',dirname(dirname(dirname(__FILE__))));
}

define('ROOT_DIR','/var/www/RedPanda_FileManager');



require_once(ROOT_PATH.'/app/classes/model.php');
require_once(ROOT_PATH.'/app/models/path.php');
require_once(ROOT_PATH.'/app/models/directory.php');
require_once(ROOT_PATH.'/app/models/file.php');
require_once(ROOT_PATH.'/app/models/image.php');
require_once(ROOT_PATH.'/app/models/imagejpg.php');
require_once(ROOT_PATH.'/app/models/imagepng.php');
require_once(ROOT_PATH.'/app/models/imagegif.php');
require_once(ROOT_PATH.'/app/models/pathfactory.php');
require_once(ROOT_PATH.'/app/models/imagefactory.php');

class TestOfDirectory extends UnitTestCase {
	function TestOfDirectory() {
        	$this->UnitTestCase('Directory test');
    	}

	function setUp() {		
        	$this->_copyDirRecursive(ROOT_PATH.'/unit_testing/tests/backup/testFolder', ROOT_PATH.'/unit_testing/tests/temp/testFolder');
    	}
    
	function tearDown() {
		$this->_removeDirRecursive(ROOT_PATH.'/unit_testing/tests/temp/testFolder');
	}
	
	function _removeDirRecursive($dir_name){
		$handle = opendir($dir_name);
		while (false !== ($file = readdir($handle))){		
			if($file=='.' || $file=='..') continue;
			$path = $dir_name.'/'.$file;
			if(is_dir($path)){
				$this->_removeDirRecursive($path);
			}else{
				unlink($path);
			}
		}
		rmdir($dir_name);	
	}
	
	function _copyDirRecursive($dir_source, $dir_destination){
		$handle = opendir($dir_source);
		
		if (!is_dir($dir_destination)) {
			mkdir($dir_destination);
		}
		
		while (false !== ($file = readdir($handle))){		
			if($file=='.' || $file=='..') continue;
			$path_source = $dir_source.'/'.$file;
			$path_destination = $dir_destination.'/'.$file;
			if(is_dir($path_source)){
				$this->_copyDirRecursive($path_source,$path_destination);
			}else{
				copy($path_source, $path_destination);
			}
		}
	}


	function testDirectoryInstance() {
		$directory = new Ab_Directory(ROOT_PATH.'/files/testFolder/');
		$this->assertTrue(is_a($directory, 'Ab_Directory'),'Routine Test:  Check instance type');
	}
	
	
	/*function testDirectoryTree(){
		$directory = new Ab_Directory(ROOT_PATH.'/files/testFolder');
		
		$tree=$directory->getDirChildren();
		
		$expected_tree=array(
				  0 => 
				    array(
				      'dir_name' => 'a',
				      'dir_path' => ROOT_PATH.'/files/testFolder/'.'a'
				      ),
				  1 => 
				    array(
				      'dir_name' => 'd',
				      'dir_path' => ROOT_PATH.'/files/testFolder/'.'d'
				      ));

		$this->assertEqual($result=$tree,$expected=$expected_tree,'Directory Tree Dirs');
	}
	
	function testDirectoryTreeFiles(){
		$directory = new Ab_Directory(ROOT_PATH.'/files/testFolder');
		
		$tree=$directory->getFileChildren();
		
		$expected_tree=array(
			  0 => 
			    array(
			      'file_name' => '06-oops.jpg',
			      'file_path' => ROOT_PATH.'/files/testFolder/'.'06-oops.jpg',
			      'file_type' => 'image'
			  ),
			  1 => 
			    array(
			      'file_name' => 'a.txt',
			      'file_path' => ROOT_PATH.'/files/testFolder/'.'a.txt',
			      'file_type' => 'file'
			   ),
			  2 => 
			    array(
			      'file_name' => 'api_integration1.png',
			      'file_path' => ROOT_PATH.'/files/testFolder/'.'api_integration1.png' ,
			      'file_type' => 'image'
			     ),
			  3 => 
			    array(
			      'file_name' => 'b.txt',
			      'file_path' => ROOT_PATH.'/files/testFolder/'.'b.txt',
			      'file_type' => 'file'
			   ),
			  4 => 
			    array(
			      'file_name' => 'header-shooting.jpg',
			      'file_path' => ROOT_PATH.'/files/testFolder/'.'header-shooting.jpg', 
			      'file_type' => 'image'
			     ) 
		);


		//$this->assertEqual($result=$tree,$expected=$expected_tree,'Directory Tree Files');
	}
	
	function testUpperDirs(){
		$directory=new Ab_Directory(ROOT_PATH.'/files/testFolder/a/c');
		
		$upper_dirs=$directory->getUpperDirs();
		
		$expected_uppper_dirs=array(
					  0 => array('dir_path'=>'files','dir_name'=>'files'),
					  1 => array('dir_path'=>'files/testFolder','dir_name'=>'testFolder'),
					  2 => array('dir_path'=>'files/testFolder/a','dir_name'=>'a'),
					  3 => array('dir_path'=>'files/testFolder/a/c','dir_name'=>'c')
  					);
  					//var_dump($upper_dirs);

		//$this->assertEqual($result=$upper_dirs,$expected=$expected_uppper_dirs,'Upper and current dir');
		
	}
	
	function testRename(){
		$file = new File(dirname(__FILE__).'/testFolder/06-oops.jpg');
		
		$result=$file->rename(dirname(__FILE__).'/testFolder/06-oops.jpg');
		
		$this->assertEqual($result=$result,true);
		
	}
	*/
	

}
?>

